package com.triones.yjj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.SnCreator;
import com.ruoyi.system.mapper.SysUserMapper;
import com.triones.yjj.entity.OrderDO;
import com.triones.yjj.entity.StaffDO;
import com.triones.yjj.entity.WithdrawalRecordDO;
import com.triones.yjj.entity.bo.WithdrawalListParams;
import com.triones.yjj.entity.vo.WithdrawalRecordVO;
import com.triones.yjj.mapper.WithdrawalRecordMapper;
import com.triones.yjj.service.WithdrawalRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 提现 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class WithdrawalRecordServiceImpl implements WithdrawalRecordService {

    private final WithdrawalRecordMapper withdrawalRecordMapper;

    private final SysUserMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean withdrawalApply(WithdrawalRecordDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        SysUser sysUser = baseMapper.selectById(loginUser.getUserId());
        if (sysUser == null) {
            throw new SecurityException("当前用户退出登录");
        }
        BigDecimal walletAmount = sysUser.getWalletAmount();
        if (walletAmount.subtract(dto.getAmountTotal()).compareTo(BigDecimal.ZERO) == -1) {
            throw new SecurityException("可提现余额不足！！！");
        }
        sysUser.setWalletAmount(walletAmount.subtract(dto.getAmountTotal()));
        baseMapper.updateById(sysUser);
        dto.setWithdrawalNo(SnCreator.getWithdrawalNo());
        dto.setUserId(loginUser.getUserId());
        dto.setStatus(1);
        dto.setCommission(BigDecimal.ZERO);
        dto.setCreateTime(DateUtil.getDateTimeString());
        return withdrawalRecordMapper.insert(dto) > 0;
    }

    @Override
    public TableDataInfo<WithdrawalRecordVO> withdrawalPageList(WithdrawalListParams params, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        LambdaQueryWrapper<WithdrawalRecordDO> lqw = Wrappers.lambdaQuery(WithdrawalRecordDO.class);
        lqw.eq(WithdrawalRecordDO::getUserId,loginUser.getUserId());
        lqw.like(ObjectUtil.isNotEmpty(params.getWithdrawalNo()),WithdrawalRecordDO::getWithdrawalNo,params.getWithdrawalNo());
        lqw.eq(ObjectUtil.isNotNull(params.getStatus()),WithdrawalRecordDO::getStatus,params.getStatus());
        if (params.getCreateTimeRange() != null && params.getCreateTimeRange().size() > 0) {
            lqw.between(WithdrawalRecordDO::getCreateTime,params.getCreateTimeRange().get(0),params.getCreateTimeRange().get(1));
        }
        if (params.getReachTimeRange() != null && params.getReachTimeRange().size() > 0) {
            lqw.between(WithdrawalRecordDO::getReachTime,params.getReachTimeRange().get(0),params.getReachTimeRange().get(1));
        }
        lqw.orderByDesc(WithdrawalRecordDO::getCreateTime,WithdrawalRecordDO::getReachTime);
        Page<WithdrawalRecordVO> result = withdrawalRecordMapper.selectVoPage(pageQuery.build(),lqw);
        return TableDataInfo.build(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean withdrawalApplyEdit(WithdrawalRecordDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        SysUser sysUser = baseMapper.selectById(loginUser.getUserId());
        if (sysUser == null) {
            throw new SecurityException("当前用户退出登录");
        }
        WithdrawalRecordDO withdrawalRecordDO = withdrawalRecordMapper.selectById(dto.getId());
        if (withdrawalRecordDO == null) {
            throw new SecurityException("提现申请记录不存在");
        }
        BigDecimal walletAmount = sysUser.getWalletAmount().add(withdrawalRecordDO.getAmountTotal());
        if (walletAmount.subtract(dto.getAmountTotal()).compareTo(BigDecimal.ZERO) == -1) {
            throw new SecurityException("可提现余额不足！！！");
        }
        sysUser.setWalletAmount(walletAmount.subtract(dto.getAmountTotal()));
        baseMapper.updateById(sysUser);
        withdrawalRecordDO.setBankName(dto.getBankName());
        withdrawalRecordDO.setSubbranch(dto.getSubbranch());
        withdrawalRecordDO.setBankAccount(dto.getBankAccount());
        withdrawalRecordDO.setCardHolding(dto.getCardHolding());
        withdrawalRecordDO.setAmountTotal(dto.getAmountTotal());
        return withdrawalRecordMapper.updateById(withdrawalRecordDO) > 0;
    }
}
